﻿@using AntDesign.Core.JsInterop.Modules.Components
@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@inherits AntDesignTestBase
@code {
    public record PersonNullable(string? Id, string Name);
    enum City
    {
        [Display(Name = "Shanghai City")]
        Shanghai,
        Zhejiang,
        Beijing,
    }

    public Select_Render_Tests()
    {
        JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
                    .SetResult(new AntDesign.JsInterop.DomRect());  
    }

    [Fact]
    public void Render_enum_select() 
    {
         //Arrange
            
        var cut = Render<EnumSelect<City>>(@<EnumSelect TEnum="City"></EnumSelect>);
        var options = cut.FindAll(".ant-select-item-option-content");

        Assert.Equal(3, options.Count);
        Assert.Equal("Shanghai City", options[0].TextContent.Trim('\n').Trim());
        Assert.Equal("Zhejiang", options[1].TextContent.Trim());
        Assert.Equal("Beijing", options[2].TextContent.Trim());
    }
    
    [Fact]
    public void Render_multiple_enum_select_with_ShowArrowIcon() 
    {
        // Arrange
        var cut = Render<EnumSelect<City>>(@<EnumSelect TEnum="City" Mode="SelectMode.Multiple" ShowArrowIcon></EnumSelect>);
        
        // Act
        var arrow = cut.Find("span.ant-select-arrow");

        // Assert
        arrow.Should().NotBeNull();
    }

    [Fact]
    public void AllowClear_button_not_rendered_for_no_data()
    {
        var dataSource = new List<PersonNullable>()
        {
            new PersonNullable("2", "Test 2"),
            new PersonNullable("3", "Test 3"),
            new PersonNullable("4", "Test 4")
        };

        var cut = Render<AntDesign.Select<string?, PersonNullable>>(
            @<AntDesign.Select DataSource="@dataSource"
                LabelName="@nameof(PersonNullable.Name)"
                ValueName="@nameof(PersonNullable.Id)"
                TItem="PersonNullable"
                TItemValue="string"
                Value="null"
                AllowClear>
        </AntDesign.Select>
    );
        //Act

        //normally blazor would rerender and in Select.OnParametersSet() 
        //would load newly set value into the SelectContent, but bUnit does 
        //not rerender, so it has to be forced. This could probably be fixed
        //by forcing StateHasChanged on the Select component, but requires 
        //investigation if it won't cause multiple re-renders.
        cut.Render(); 
        //Assert
        cut.Invoking(c => c.Find("span.ant-select-clear"))
                .Should().Throw<Bunit.ElementNotFoundException>();

    }

    [Fact]
    public void AutoFocus_focus_set_after_render()
    {
        //Arrange
#if !NET6_0_OR_GREATER
#pragma warning disable CS0618 // Type or member is obsolete
        JSInterop.SetupVoid(JSInteropConstants.Focus, _ => true).SetVoidResult();
#pragma warning restore CS0618 // Type or member is obsolete
#endif

        var dataSource = new List<PersonNullable>()
        {
            new PersonNullable("2", "Test 2"),
            new PersonNullable("3", "Test 3"),
            new PersonNullable("4", "Test 4"),
        };
        var cut = Render<AntDesign.Select<string?, PersonNullable>>(
            @<AntDesign.Select DataSource="@dataSource"
                LabelName="@nameof(PersonNullable.Name)"
                ValueName="@nameof(PersonNullable.Id)"
                TItem="PersonNullable"
                TItemValue="string"
                Value="null"
                AutoFocus="true">
        </AntDesign.Select>
    );

        //Assert
#if NET6_0_OR_GREATER
		JSInterop.VerifyFocusAsyncInvoke();
#else
        JSInterop.VerifyInvoke(JSInteropConstants.Focus);
#endif
    }
}